package smsapi.api;


import org.springframework.http.*;
import org.springframework.stereotype.Component;
import smsapi.ApiException;
import smsapi.model.SendRequest;
import smsapi.model.SendResponse;
import smsapi.model.SendResponseSync;
import smsapi.model.SendSms;

@Component
public class SendSyncApi extends ApiBase {

   final String localVarPath = "/api/SendSync";
    /**
     * 发送短信 异步发送
     *
     * @param sendSms
     * @param timestamp 时间戳 (required)
     * @param nonce 仅仅被使用一次 (required)
     * @param token 令牌 (required)
     * @return SendResponse
     * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
     */
    public SendResponseSync sendSms(SendSms sendSms, String timestamp, String nonce, String token) throws ApiException {
        sendSmsValidateBeforeCall(sendSms, timestamp, nonce, token);

        final StringBuilder url = new StringBuilder();
        url.append(basepath).append(localVarPath);
        HttpHeaders headers = getHttpHeaders(sendSms, timestamp, nonce, token);
        HttpEntity<?> entity = new HttpEntity<>(sendSms, headers);
        ResponseEntity<SendResponseSync> resultResponseEntity = restTemplate.exchange(url.toString(), HttpMethod.POST, entity, SendResponseSync.class);
        if (resultResponseEntity.getStatusCode() == HttpStatus.OK) {
            return resultResponseEntity.getBody();
        }else {
            throw new ApiException(resultResponseEntity.getStatusCodeValue(),resultResponseEntity.getStatusCode().getReasonPhrase(),null,resultResponseEntity.getBody().toString());
        }
    }

    private void sendSmsValidateBeforeCall(SendSms request, String timestamp, String nonce, String token) throws ApiException {

        // verify the required parameter 'request' is set
        if (request == null) {
            throw new ApiException("Missing the required parameter 'request' when calling SendSync");
        }

        // verify the required parameter 'timestamp' is set
        if (timestamp == null) {
            throw new ApiException("Missing the required parameter 'timestamp' when calling SendSync");
        }

        // verify the required parameter 'nonce' is set
        if (nonce == null) {
            throw new ApiException("Missing the required parameter 'nonce' when calling SendSync");
        }

        // verify the required parameter 'companyNo' is set
        if (companyNo == null) {
            throw new ApiException("Missing the required parameter 'companyNo' when calling SendSync");
        }

        // verify the required parameter 'token' is set
        if (token == null) {
            throw new ApiException("Missing the required parameter 'token' when calling SendSync");
        }

    }
}
